package org.ftc.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.ftc.domain.Committee;
import org.ftc.service.CommitteeService;
import org.ftc.service.FtcLogService;
import org.ftc.service.FtcLogService.Log;
import org.ftc.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller 
public class CommitteeController {
	@Autowired
	UsersService usersService;
	@Autowired
	CommitteeService committeeService;
	@Autowired
	FtcLogService logService;
	
	@RequestMapping(value = "/committee.htm")
	public String codeCause(ModelMap model) {
		return "committee/list";
	}

	@RequestMapping(value = "/committee/list.json", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> find(ModelMap model) {
		logService.before();
		Map<String, Object> json = new HashMap<String, Object>();
		List<Committee> data = committeeService.findAll();
		json.put("rows", data);
		logService.save(Log.QUERY, this.getClass().toString());
		return json;
	}

	@RequestMapping(value = "/committee/update.json", method = RequestMethod.POST)
	public @ResponseBody Object save(Committee entity,BindingResult binding, ModelMap model) {
		logService.before();
		if(entity==null || StringUtils.isBlank(entity.getName())){
			return true;
		}
		if(StringUtils.isBlank(entity.getCode())){
			committeeService.save(entity);
			logService.save(Log.INSERT, entity.getCode(), this.getClass().toString() ,ToStringBuilder.reflectionToString(entity));
		}else{
			Committee committee = committeeService.get(entity.getCode());
			committee.setName(entity.getName());
			committee.setSort(entity.getSort());
			committeeService.update(committee);
			logService.save(Log.UPDATE, entity.getCode(), this.getClass().toString() ,ToStringBuilder.reflectionToString(entity));
		}
		return entity;
	}
	
	@RequestMapping(value = "/committee/delete/{committeeSeq}.json")
	public @ResponseBody boolean delete(@PathVariable String committeeSeq) {
		logService.before();
		Boolean s= false;
		if(StringUtils.isBlank(committeeSeq)){
			return s;
		}
		s = committeeService.delete(committeeSeq);
		logService.save(Log.DELETE, committeeSeq, this.getClass().toString());
		return s;  
	}
	
	@RequestMapping(value = "/committee/findAll.json", method = RequestMethod.GET)
	public @ResponseBody
	List<Committee> findAll() {
		return committeeService.findAll();
	}
	
	@RequestMapping(value = "/committee/findCkd.json", method = RequestMethod.GET)
	public @ResponseBody
	List<Committee> findCkd() { 
		return committeeService.findCkd();
	}
	
	@RequestMapping(value = "/committee/findByCode.json", method = RequestMethod.GET)
	public @ResponseBody
	List<Committee> findByCode(String code) { 
		return committeeService.findByCode(code);
	}

}
